/*     */ package com.newcrm.webapi.sub;
/*     */ 
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.db.CustomerManager;
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import com.newcrm.util.MD5;
/*     */ import com.newcrm.util.Util;
/*     */ import com.newcrm.webapi.AbstractAction;
/*     */ import com.newcrm.webapi.AbstractHttpServlet;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import javax.servlet.RequestDispatcher;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.apache.commons.fileupload.FileItem;
/*     */ import org.apache.commons.fileupload.FileUploadException;
/*     */ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
/*     */ import org.apache.commons.fileupload.servlet.ServletFileUpload;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class UpdateAvatar extends AbstractAction
/*     */ {
/*  36 */   private static Logger logger = Logger.getLogger(UpdateAvatar.class
/*  37 */     .getName());
/*     */ 
/*  39 */   private int id = 0;
/*  40 */   private String path = "";
/*  41 */   private boolean isPerson = true;
/*     */ 
/*     */   public UpdateAvatar(AbstractHttpServlet servlet) {
/*  44 */     super(servlet);
/*     */   }
/*     */ 
/*     */   public UpdateAvatar(AbstractHttpServlet servlet, int id, String uploadPath, boolean isPerson)
/*     */   {
/*  49 */     super(servlet);
/*  50 */     this.id = id;
/*  51 */     this.path = uploadPath;
/*  52 */     this.isPerson = isPerson;
/*     */   }
/*     */ 
/*     */   public void parse(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException, ServletException, NotFoundException
/*     */   {
/*  59 */     if (this.id < 0) {
/*  60 */       request.getRequestDispatcher("/errorpage.jsp").forward(request, 
/*  61 */         response);
/*  62 */       return;
/*     */     }
/*     */ 
/*  65 */     DiskFileItemFactory fac = new DiskFileItemFactory();
/*  66 */     ServletFileUpload upload = new ServletFileUpload(fac);
/*  67 */     upload.setHeaderEncoding("utf-8");
    List fileList;
/*     */     try
/*     */     {
/*  70 */       fileList = upload.parseRequest(request);
/*     */     }
/*     */     catch (FileUploadException e1)
/*     */     {
/*     */
/*  72 */       logger.warn("upload file failed", e1);
/*     */       return;
/*     */     }
/*  76 */     Iterator it = fileList.iterator();
/*  77 */     String imgPath = "";
/*  78 */     while (it.hasNext()) {
/*  79 */       FileItem item = (FileItem)it.next();
/*  80 */       if (!item.isFormField())
/*     */       {
/*  84 */         String fileName = item.getName();
/*  85 */         if ((fileName != null) && (!fileName.trim().equals("")))
/*     */         {
/*  89 */           String extName = "";
/*  90 */           if (fileName.lastIndexOf(".") >= 0) {
/*  91 */             extName = fileName.substring(fileName.lastIndexOf("."));
/*     */           }
/*     */ 
/*  94 */           if (!Util.isImageFile(extName, item.get())) {
/*  95 */             request.getRequestDispatcher("/errorpage.jsp").forward(request, 
/*  96 */               response);
/*  97 */             return;
/*     */           }
/*  99 */           extName = extName.toLowerCase();
/*     */ 
/* 101 */           imgPath = this.id + "_" + MD5.getMD5(fileName) + extName;
/* 102 */           File file = new File(this.path, imgPath);
/*     */           try {
/* 104 */             item.write(file);
/*     */           } catch (Exception e) {
/* 106 */             logger.warn("write image file failed", e);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/* 109 */     User loginUser = super.getLoginUser(request);
/*     */ 
/* 111 */     CustomerManager manager = new CustomerManager();
/* 112 */     manager.updateImgPath(loginUser.userId, this.id, imgPath);
/* 113 */     response.setContentType("text/html;charset=utf-8");
/* 114 */     PrintWriter out = response.getWriter();
/* 115 */     StringBuffer msg = new StringBuffer(
/* 116 */       "<img class='avatar' height='55' src='");
/* 117 */     msg.append(request.getContextPath()).append("/");
/*     */ 
/* 119 */     if (this.isPerson)
/* 120 */       msg.append("images/upload/people/");
/*     */     else {
/* 122 */       msg.append("images/upload/company/");
/*     */     }
/* 124 */     msg.append(imgPath).append("' width='55'>");
/* 125 */     out.println(msg);
/* 126 */     out.flush();
/* 127 */     out.close();
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.sub.UpdateAvatar
 * JD-Core Version:    0.6.1
 */